﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.CongNo
{
    public partial class frmChiTiet : Form
    {
        #region khởi tạo
        DateTime date;
        public static bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.HDBanBuon[] bb1;
        Entities.HDBanLe[] bl1;
        Entities.CongNo[] congnochitiet;
        Entities.CongNo cn;
        Entities.HoaDonNhap[] hdn1;
        Entities.KhachHangTraLaiBanBuon[] khbb1;
        Entities.KhachHangTraLaiBanLe[] khbl1;
        Entities.KhachHang kh1;
        Entities.NhaCungCap ncc1;
        Entities.TraLaiNCC[] tlncc1;
        Entities.PhieuTTCuaKH[] phieuttcuakh1;
        Entities.PhieuTTNCC[] phieuttncc1;
        Entities.KhachHang[] khh;
        Entities.NhaCungCap[] nccc;
        int i = 0;

        string hanhDong;
        public frmChiTiet(Entities.CongNo[] congnochitietkh1, Entities.HDBanBuon[] bb1, Entities.HDBanLe[] bl1, Entities.PhieuTTCuaKH[] phieuttcuakh1,
            Entities.KhachHangTraLaiBanBuon[] khbb1, Entities.KhachHangTraLaiBanLe[] khbl1, Entities.KhachHang kh, string sddk, string sdck)
        {
            InitializeComponent();
            this.bb1 = bb1;
            this.bl1 = bl1;
            this.congnochitiet = congnochitietkh1;
            this.khbb1 = khbb1;
            this.khbl1 = khbl1;
            this.kh1 = kh;
            this.phieuttcuakh1 = phieuttcuakh1;
            lbGiaTriMa.Text = kh1.MaKhachHang;
            lbGiaTriTen.Text = kh1.Ten;
            lbGiaTriDiaChi.Text = kh1.DiaChi;
            txtDuDauKy.Text = Format(double.Parse(sddk));
            txtDuCuoiKy.Text = Format(double.Parse(sdck));
            congnochitiet[congnochitiet.Length - 1].DiaChi = "Tổng";
            congnochitiet[congnochitiet.Length - 1] = new Entities.CongNo("Tổng Cộng", "", Format(Chi()), Format(Thu()), "");

            dgvHienThi.DataSource = congnochitiet;
            fix();
            Ton();
            date = Entitystatic.date;
        }
        public frmChiTiet(Entities.CongNo[] congnochitietncc,
            Entities.HoaDonNhap[] hdn1, Entities.PhieuTTNCC[] phieuttncc1, Entities.TraLaiNCC[] tlncc1, Entities.NhaCungCap ncc1, string sddk, string sdck)
        {
            InitializeComponent();
            this.congnochitiet = congnochitietncc;
            this.hdn1 = hdn1;
            this.tlncc1 = tlncc1;
            this.ncc1 = ncc1;
            this.phieuttncc1 = phieuttncc1;
            txtDuDauKy.Text = Format(double.Parse(sddk));
            txtDuCuoiKy.Text = Format(double.Parse(sdck));
            lbGiaTriMa.Text = ncc1.MaNhaCungCap;
            lbGiaTriTen.Text = ncc1.TenNhaCungCap;
            lbGiaTriDiaChi.Text = ncc1.DiaChi;
            congnochitiet[congnochitiet.Length - 1] = new Entities.CongNo("Tổng Cộng", "", Format(Thu()), Format(Chi()), "");
            dgvHienThi.DataSource = congnochitiet;
            fix();
            TonNCC();
            date = Entitystatic.date;
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.CongNo.NgayLap].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.MaTruongNgam].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.DiaChi].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.Ton].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhNo].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhCo].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.NgayLap].HeaderText = "Ngày Lập";
            dgvHienThi.Columns[Common.Constants.CongNo.MaTruongNgam].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.CongNo.DiaChi].HeaderText = "Địa Chỉ";
            dgvHienThi.Columns[Common.Constants.CongNo.Ton].HeaderText = "Còn Lại";
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhNo].HeaderText = "Phát Sinh Nợ";
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhCo].HeaderText = "Phát Sinh Có";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region format
        public string Format(double a)
        {
            if (a >= 0 && a < 10)
                return a.ToString();
            if (a < 0)
                return String.Format("{0,-0:0,0}", a);
            else
                return String.Format("{0:0,0}", a);
        }
        #endregion

        #region thu Chi
        public double Thu()
        {
            double tongthu = 0;
            for (int i = 0; i < congnochitiet.Length - 1; i++)
            {
                tongthu += Convert.ToDouble(congnochitiet[i].PhatSinhNo);
            }
            return tongthu;
        }

        public double Chi()
        {
            double tongchi = 0;
            for (int i = 0; i < congnochitiet.Length - 1; i++)
            {
                tongchi += Convert.ToDouble(congnochitiet[i].PhatSinhCo);
            }
            return tongchi;
        }
        #endregion

        #region tồn nhà cung cấp
        public void TonNCC()
        {
            Entities.CongNo[] cn = (Entities.CongNo[])dgvHienThi.DataSource;
            double ton = 0;
            for (int i = 0; i < congnochitiet.Length - 1; i++)
            {
                ton = Convert.ToDouble(ton) - Convert.ToDouble(congnochitiet[i].PhatSinhNo) + Convert.ToDouble(congnochitiet[i].PhatSinhCo);
                cn[i].Ton = Format(ton);
            }
        }
        #endregion

        #region Tồn khách hàng
        public void Ton()
        {
            Entities.CongNo[] cnkh = (Entities.CongNo[])dgvHienThi.DataSource;
            double ton = 0;
            for (int i = 0; i < congnochitiet.Length - 1; i++)
            {
                ton = Convert.ToDouble(ton) + Convert.ToDouble(congnochitiet[i].PhatSinhNo) - Convert.ToDouble(congnochitiet[i].PhatSinhCo);
                cnkh[i].Ton = Format(ton);
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Xử lý grid view
        void XuLy()
        {
            if (i < 0)
                return;
            try
            {

                bool kt = true;
                string objname = congnochitiet[i].TruongNgam;
                string madoituong = congnochitiet[i].MaTruongNgam;
                switch (objname)
                {
                    case "BanBuon":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < bb1.Length; j++)
                            {
                                if (bb1[j].MaHDBanBuon == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            } if (kt == true)
                            {
                                BanBuon.frmXuLy b = new BanBuon.frmXuLy(bb1[so]);
                                b.ShowDialog();
                            }
                            break;
                        }
                    case "PhieuTTCuaKH":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < phieuttcuakh1.Length; j++)
                            {
                                if (phieuttcuakh1[j].MaPhieuTTCuaKH == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt == true)
                            {
                                PhieuTTCuaKH.frmXuLy b = new PhieuTTCuaKH.frmXuLy(phieuttcuakh1[so], "Update");
                                b.ShowDialog();
                            }
                            break;
                        }
                    case "BanLe":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < bl1.Length; j++)
                            {
                                if (bl1[j].MaHDBanLe == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            } if (kt == true)
                            {
                                BanLe.frmXuLy b = new BanLe.frmXuLy(bl1[so]);
                                b.ShowDialog();
                            }
                            break;
                        }
                    case "KhachHangTraLaiBanBuon":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < khbb1.Length; j++)
                            {
                                if (khbb1[j].MaKhachHangTraLaiBanBuon == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            } if (kt == true)
                            {
                                //GUI.KhachHangTraLaiBanBuon.frmXuLy b = new GUI.KhachHangTraLaiBanBuon.frmXuLy(Common.Constants.GiaTriCacForm.SuaKhachHangTraLaiBanBuon, khbb1[so]);
                                //b.ShowDialog();
                            }
                            break;
                        }
                    case "KhachHangTraLaiBanLe":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < khbl1.Length; j++)
                            {
                                if (khbl1[j].MaKhachHangTraLaiBanLe == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            } if (kt == true)
                            {
                                //GUI.KhachHangTraLaiBanLe.frmXuLy b = new GUI.frmXuLy.frmXuLyTraLaiBanLe(Common.Constants.GiaTriCacForm.SuaKhachHangTraLaiBanLe, khbl1[so]);
                                //b.ShowDialog();
                            }
                            break;
                        }
                    case "HoaDonNhap":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < hdn1.Length; j++)
                            {
                                if (hdn1[j].MaHoaDonNhap == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt == true)
                            {
                                //GUI.NhapKho.frmXuLy b = new GUI.NhapKho.frmXuLy(Common.Constants.GiaTriCacForm.SuaHoaDonNhap, hdn1[so]);
                                //b.ShowDialog();
                            }
                            break;
                        }
                    case "PhieuTTNCC":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < phieuttncc1.Length; j++)
                            {
                                if (phieuttncc1[j].MaPhieuTTNCC == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt == true)
                            {
                                GUI.PhieuTTNCC.frmXuLy b = new GUI.PhieuTTNCC.frmXuLy(phieuttncc1[so], "Update");
                                b.ShowDialog();
                            }
                            break;
                        }
                    case "TLNCC":
                        {
                            kt = false;
                            int so = 0;
                            for (int j = 0; j < tlncc1.Length; j++)
                            {
                                if (tlncc1[j].MaHDTraLaiNCC == madoituong)
                                {
                                    so = j;
                                    kt = true;
                                    break;
                                }
                            }
                            if (kt == true)
                            {
                                GUI.TraLaiNhaCungCap.frmXuLy b = new GUI.TraLaiNhaCungCap.frmXuLy(tlncc1[so]);
                                b.ShowDialog();
                            }
                            break;
                        }
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
            }

        }
        #endregion

        #region Event
        private void frmChiTiet_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmChiTiet_FormClosing);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvhienthi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvhienthi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvhienthi_KeyDown);
        }

        void dgvhienthi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                XuLy();
            }
        }

        void dgvhienthi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            XuLy();
        }

        void dgvhienthi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void frmChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion